package expression

import (
	"fmt"
	"mini-sql/model"
)

func GetTableName(expr *model.Expr) (string, error) {
	switch expr.ExprType {
	case model.ExprTypeNameIdList:
		{
			if len(expr.Children) != 1 {
				return "", fmt.Errorf("only support select from one table")
			}
			return GetTableName(expr.Children[0])
		}
	case model.ExprTypeStr:
		return expr.StrValue, nil
	case model.ExprTypeNameId:
		return expr.StrValue, nil
	}
	return "", fmt.Errorf("GetTableName: unsupported expr %v", expr)
}

func GetFieldName(expr *model.Expr) (string, error) {
	switch expr.ExprType {
	case model.ExprTypeNameId:
		return expr.StrValue, nil
	case model.ExprTypeStar:
		return expr.StrValue, nil
	}
	return "", fmt.Errorf("GetFieldName: unsupported expr %v", expr)
}
